using System;
using System.Collections.Generic;
using System.Text;
using Fieldbook.Entities;
using Fieldbook.Common;
using Fieldbook.DAL;

namespace Fieldbook.Business
{
    public static class ResourceController
    {
        public static List<ResourceCategory> LoadResourceCategories(long userID)
        {
            List<ResourceCategory> res = null;
            try
            {
                res = ResourceCategoryDAO.LoadResourceCategories(userID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading resource categories", ex);
            }
            return res;
        }

        public static bool InsertResourceCategory(ResourceCategory category)
        {
            bool res = false;
            try
            {
                res = ResourceCategoryDAO.InsertResourceCategory(category);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error inserting resource category", ex);
            }
            return res;
        }

        public static bool InsertResourceCategory(string description)
        {
            ResourceCategory category = new ResourceCategory();
            category.Description = description;
            return InsertResourceCategory(category);
        }

        public static bool InsertResourceCategory(string description, long userID)
        {
            bool res = false;
            try
            {
                long? houseHoldID = HouseholdDAO.GetHouseHoldID(userID);
                if (houseHoldID != null)
                {
                    ResourceCategory category = new ResourceCategory();
                    category.Description = description;
                    category.HouseHoldID = houseHoldID;
                    res = InsertResourceCategory(category);
                }
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error inserting resource category", ex);
            }
            return res;
        }

        public static List<Resource> LoadResources(long userID)
        {
            List<Resource> res = null;
            try
            {
                res = ResourceDAO.LoadResources(userID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading resources", ex);
            }
            return res;
        }

        public static List<Resource> LoadResources(long userID, long categoryID)
        {
            List<Resource> res = null;
            try
            {
                res = ResourceDAO.LoadResources(userID, categoryID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading resources", ex);
            }
            return res;
        }

        public static bool InsertResource(Resource resource)
        {
            bool res = false;
            try
            {
                res = ResourceDAO.InsertResource(resource);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error inserting resource", ex);
            }
            return res;
        }

        public static bool InsertResource(long categoryID, int measureUnitID, string description)
        {
            Resource resource = new Resource();
            resource.CategoryID = categoryID;
            resource.MeasureunitID = measureUnitID;
            resource.Description = description;
            return InsertResource(resource);
        }

        public static bool InsertResource(long categoryID, int measureUnitID, string description, long userID)
        {
            Household hHold = null;
            try
            {
                string criteria = String.Format(" WHERE MEMBERID = {0}", userID);
                hHold = HouseholdDAO.LoadHouseholds(criteria, "")[0];
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading household category", ex);
            }
            if (hHold == null)
            {
                return false;
            }
            Resource resource = new Resource();
            resource.CategoryID = categoryID;
            resource.MeasureunitID = measureUnitID;
            resource.Description = description;
            resource.HouseholdID = hHold.Headid;
            return InsertResource(resource);
        }
    }
}
